#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::begin;
using std::end;

int readByWord()
{
	cout << "输入文本：" << endl;
	string word;
	while (cin >> word)
		if (word == "\0")
			return 0;
		cout << word;
	return 0;
}

int t3_5()
{
	cout << "请输入字符串：" << endl;
	string s0, sum;
	while (getline(cin, s0))
	{
		cout << "s0: " << s0 << endl;
		if (s0 == "0")
			break;
		sum += (s0 + " ");
	}
	cout << "sum: " << sum << endl;
	return 0;
}

int t3_2_2()
{
	string line;
	cout << "请输入文本：（English）" << endl;
	while (getline(cin, line))
		if (line == "0")
			return 0;
		cout << line << endl;
	return 0;
}

int t3_6()
{

	string s0("amd is ok!!!");
	for (auto &c: s0)
		c = toupper(c);
	cout << s0 << endl;
	return 0;
}

string t3_10()
{

	string s1;
	getline(cin, s1);
	string result;
	for (auto c: s1)
		if (!ispunct(c))
			result += c;
	cout << result << endl;;
	return result;
}

int t3_11()
{
	/*
	 * auto 一般自动忽略顶层const
	 * 1. 引用时保留
	 * 2. 显式声明
	 */
	const string s0 = "hello";

	for (char c: s0)
	{
		c = 'X';
	}
	return 0;
}

int t3_14()
{
	int i;
	vector<int> vec;
	cout << "please inter ints: " << endl;
	while (cin >> i)
	{
		if (i == 0)
			break;
		vec.push_back(i);
	}

	for (auto i: vec)
		cout << " " << i;
	return 0;
}

int t3_15()
{
	string s0;
	vector<string> vec;
	cout << "please inter strings: " << endl;
	while (getline(cin, s0))
	{
		if (s0 == "0")
			break;
		vec.push_back(s0);
	}

	for (auto i: vec)
		cout << "\n" << i;
	return 0;
}

int t3_23()
{
	/*
	 * vector for循环中不要vector中添加元素
	 */

	vector<int> vec{0,1,2,3,4,5,6,7,8,9};
	for (auto &i: vec)
	{
		i *= 2;
	}

	for (auto i: vec)
	{
		cout << i << " ";
	}
	return 0;
}

int binarySearch(int sought)
{
	vector<int> vec{0, 3, 5, 6, 9, 10, 7, 8};

	auto beg = vec.begin(), end = vec.end();
	auto mid = vec.begin() + (end - beg) / 2;
	// 查找sought
	while (mid != end && *mid != sought)
	{
		if (sought < *mid)
			end = mid;
		else
			beg = mid + 1;
		// 因为迭代器不支持直接相加
		mid = beg + (end - beg) / 2;
	}

	if (*mid == sought)
	{
		cout << "找到了sought" << "(" << sought << ")"
				 << "索引是：" << mid - vec.begin() << endl;
		return true;
	}
	return false;
}

int t3_33()
{
	// 函数体内定义的不会随机初始化
	unsigned scores[11]{};
	unsigned grade;
	while (cin >> grade)
	{
		if (grade == 0)
			break;
		if (grade <= 100 )
			++scores[grade/10];
	}

	for (int i = 0; i < 11; i++)
		cout << scores[i] << " ";
	return 0;
}

int t3_41()
{
	/*
	 * vector与C数组的相互初始化
	 */
	int arr[]{0,1,2,3,4,5};
//	int arr2[7];  // 不初始化则必须指定长度
	vector<int> vec(arr, arr + 6);
	for (int i: vec)
		cout << i << " ";
	return 0;
}

int t3_45()
{
	/*
	 * 指针也是迭代器
	 */
	cout << "---------------Version1---------------" << endl;
	cout << "请输入整数：" << endl;
	int ia1[3][4];
	using rowtype = int(*)[4];
	for (rowtype p = ia1; p < ia1 + 3; ++p)
	{
		for (int* q = *p; q < *p + 4; ++q)
		{
			cin >> *q;
		}
	}

	for (auto p = ia1; p < ia1 + 3; ++p)
	{
		for (auto q = p; q < p + 4; ++q)
		{
			cout << *q;
		}
	}


	cout << "请输入整数：" << endl;
	int ia[3][4];
	for (auto p = begin(ia); p != end(ia); ++p)
	{
		// q指向内层数组的首元素
		for (auto q = begin(*p); q != end(*p); ++q)
		{
			cin >> *q;
		}
	}

	for (auto p = begin(ia); p != end(ia); ++p)
	{
		for (auto q = begin(*p); q != end(*p); ++q)
		{
			cout << *q;
		}
	}
	return 0;
}

int main03()
{
//	t3_2_2();
//	t3_5();
//	t3_6();
//	t3_10();
//	t3_11();
//	t3_14();
//	t3_15();
//	t3_23();
//	binarySearch(5);
//	t3_33();
//	t3_41();
	t3_45();
	return 0;
}
